package com.kedacom.ctsp.registry.controller;


import com.kedacom.ctsp.lang.StringUtil;
import com.kedacom.ctsp.logging.AccessLogger;
import com.kedacom.ctsp.registry.entity.Account;
import com.kedacom.ctsp.registry.service.UserAccountService;
import com.kedacom.ctsp.web.controller.message.ResponseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import static com.kedacom.ctsp.web.controller.message.ResponseMessage.ok;

/**
 * @author sunchenjie
 * @date 2017/11/15 0014
 */
@RequestMapping("${ctsp.web.mappings.registry.account:account}")
@ResponseBody
@AccessLogger("user-account")
@Api(tags = "user-account", description = "用户注册账号相关")
public class UserAccountController {

    @Autowired(required = false)
    private UserAccountService accountService;


    @GetMapping("/unique")
    @ApiOperation("检测用户名/邮箱唯一性")
    public ResponseMessage checkUnique(@RequestParam(required = false) String username,
                                       @RequestParam(required = false) String email) {
        if (!StringUtil.isNullOrEmpty(username)) {
            accountService.checkUsernameUnique(username);
        }
        if (!StringUtil.isNullOrEmpty(email)) {
            accountService.checkEmailUnique(email);
        }
        return ok();
    }


    /**
     * 通过用户名获取邮箱
     *
     * @param
     */
    @GetMapping("/email")
    @ApiOperation("通过用户名获得邮箱")
    public ResponseMessage getMailByUsername(@RequestParam String username) {
        Long userId = accountService.getUserIdByUsername(username);
        Assert.notNull(userId, "用户不存在");
        String email = accountService.getUserEmailAndValidate(userId);
        return ok(mosaicEmail(email));
    }


    /**
     * 通过url激活账号
     * 通过前台转发
     */
    @GetMapping("/activate/forward")
    @ApiOperation("通过url激活账号")
    public ResponseMessage activateAccountByVerifyUrl(@RequestParam String code) {
        Long userId = accountService.activateAccountByVerifyUrl(code);
        return ok(userId);
    }


    /**
     * 授权码修改密码验证
     */
    @PostMapping("/retrieve-pwd/verify")
    @ApiOperation("验证邮件发送授权码")
    public ResponseMessage verifyPwdByAuthenticationCode(@RequestBody Account account) {
        Long userId = accountService.verifyByAuthenticationCode(account.getUsername(), account.getCode());
        return ok(userId);
    }

    /**
     * 授权码修改密码
     */
    @PostMapping("/retrieve-pwd/modify")
    @ApiOperation("通过邮件发送授权码修改密码")
    public ResponseMessage modifyPwdByAuthenticationCode(@RequestBody Account account) {
        boolean b = accountService.modifyPwdByAuthenticationCode(account.getUsername(), account.getPassword(), account.getCode());
        return ok(b);
    }


    private String mosaicEmail(String email) {
        String end = email.substring(email.indexOf("@") - 1, email.length());
        return email.substring(0, 1) + "****" + end;
    }
}


